/**
 * Created by Administrator on 2017/9/7 0007.
 */


//系统启动获取专辑ID
$().ready(function () {
    //给开关加默认值
    $("#isZip").val(1);

    //将选中的图片id临时存放起来
    $('#addCate').mousedown(function (e) {
        if (3 == e.which) {//鼠标右击
            $('#tempText').val(e.target.src + '@_@' + e.target.name);
        }
    })

    /**
     * 右键菜单
     */
    $('#addCate').contextmenu({
        target: '#context-menu2',
        onItem: function (context, e) {
            var cate_menu = e.target.parentNode.id
            var cate_id = cate_menu.split("_")[1];
            var value = $('#tempText').val().split("@_@");
            $('img#' + cate_id).attr("src", value[0]).attr("name", value[1]);
        }
    });

    /**
     * 选择分类的时候，自动添加封面处理
     */
    $('#cateList').change(function (obj) {
        var selectIndex = $('#cateList').find("option");
        if (selectIndex !== null) {
            $.each(selectIndex, function (index, data) {
                // var op = data[index];
                if (data.selected) {
                    var div = $("." + data.value);
                    if (div.length <= 0) {
                        var temp = '<div class="' + data.value + '"><label class="col-lg-1 control-label">【' + data.text + '】封面</label>' +
                            '<div class="col-lg-1">' + '<img  id="' + data.value + '" style="width: 60px;height: 80px;" class="album_conver">' +
                            '</div>';
                        $('#cateConver').append(temp);

                        var menuTemp = '<li id="menu_' + data.value + '"><a  tabindex="-1">设为【' + data.text + '】封面</a></li>'
                        $('#role_menu').append(menuTemp);
                    }
                } else {
                    var div = $("." + data.value);
                    var menu = $('#menu_' + data.value);
                    if (menu.length > 0) {
                        menu.remove();
                    }
                    if (div.length > 0) {
                        div.remove();
                    }
                }
            })
        }
    })
})

//点击开关改变值
$('#isZip').on('switch-change', function (e, data) {
    if (data.value == true) {
        $("#isZip").val(1);
    } else {
        $("#isZip").val(0);
    }
});
//上传文件
function imagetUpload() {
    $.bootstrapLoading.start({loadingTips: "正在处理数据，请稍候..."});
    var formData = new FormData();
    var files = $('#fileName')[0].files;
    for (var i = 0; i < files.length; i++) {
        formData.append("fileName", $('#fileName')[0].files[i]);
    }
    formData.append("albId", $('#albId').val());
    formData.append("isZip", $("#isZip").val());
    $.ajax({
        url: "/view/uploadList",
        type: "POST",
        data: formData,
        //必须false才会自动加上正确的Content-Type
        contentType: false,
        //必须false才会避开jQuery对 formdata 的默认处理
        //XMLHttpRequest会对 formdata 进行正确的处理
        processData: false,
        success: function (data) {
            var ids = data.data.ids;

            $('#uploadImages').append("");
            for (var key in ids) {
                var temp = '<div class="col-lg-4"><img src="' + ids[key] + '" width="60px" height="80px" name="' + key + '" class="prettyPhoto[pp_gal]" >' +
                    ' <div class="checkbox"><input type="hidden" id="uploadImg" value="' + key +
                    '"> <label><input id="' + key + '" type="checkbox"/>免费</label></div></div>'
                $('#uploadImages').append(temp);
            }
        },
        complete: function () {
            $.bootstrapLoading.end();
        }
    });
}
/**
 * 获取免费图片id并传递给主页面
 */
function getFree() {
    var checkbox = $(':checkbox[id]');
    var hidden = $(':hidden[id=uploadImg]');
    var ids = "";
    for (var i = 0; i < checkbox.size(); i++) {
        var check = $('#' + checkbox[i].id + '')
        if (check.is(":checked")) {
            for (var j = 0; j < hidden.size(); j++) {
                if (checkbox[i].id === hidden[j].value) {
                    ids += hidden[j].value + ",";
                }
            }
        }
    }
    $('#imageId').val(ids);
    $("#addCate").modal('hide');
}


/**
 * 增加修改专辑
 * @param ids
 */
function addAlbum() {
    var cateList = $('#cateList').val();
    var cateIds = '';
    if (cateList !== null && cateList !== undefined) {
        for (var i = 0; i < cateList.length; i++) {
            cateIds += cateList[i] + ',';
        }
    }
    var convers = $('img.album_conver');
    var converIds = {};
    if (convers !== null && convers.length > 0) {
        $.each(convers, function (index, conver) {
            converIds[conver.id] = conver.name;// = conver.value + ",";
        })
    }
    var data = {
        'albId': $('#albId').val(),
        'albName': $('#albName').val(),
        'modelId': $('#modelId').val(),
        'albDescription': $('#albDescription').val(),
        'originalPrice': $('#originalPrice').val(),
        'presentPrice': $('#presentPrice').val(),
        'cateIds': cateIds,
        'converIds': JSON.stringify(converIds),
        'imgIds': $('#imageId').val()
    }
    $.ajax({
        url: "/view/saveOrUpdateAlbum",
        type: "POST",
        data: data,
        success: function (data) {
            if (data.data) {
                window.location.href = '/view/statement?pageNumber=1&pageSize=10';
            }
        }
    });
}
function cancell() {
    window.location = '/view/statement?pageNumber=1&pageSize=10';
}